package com.example.administrator.myapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    private TextView contentTv;
    private Button setFontSizeBt;
    int textSize = 1;
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        initData();
        initEvant();
    }

    private void initView() {
        contentTv = findViewById(R.id.tv);
        setFontSizeBt = findViewById(R.id.bt);

    }

    private void initData() {


    }

    private void initEvant() {
        setFontSizeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAlertDialogFontSize();
            }
        });


    }

    //设置弹出框为单选对话框的内容
    private void setAlertDialogFontSize() {
        final int[] textSizeArr = {10, 20, 30, 40, 50};

        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("设置字体大小")
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(new String[]{"小号", "默认", "中号", "大号", "超大"}, textSize, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        textSize = i;
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        contentTv.setTextSize(textSizeArr[textSize]);
                        alertDialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                });
        alertDialog = builder.create();//根据builder创建AlertDialog
        alertDialog.show();
    }
}
